<template>
  <div class="login">
    <a-modal :visible="visible" @cancel="updateCancel" @ok="handleSubmit" title="登录">
      <a-form>
        <a-form-item label="用户名">
          <a-input v-model="username" />
        </a-form-item>
        <a-form-item label="密码">
          <a-input v-model="password" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import Nav from "../nav/Nav";
import { login } from "@/api/user";
import bus from "../../assets/bus";
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
      // visible:this.visible
    };
  },
  props: {
    // eslint-disable-next-line vue/no-dupe-keys
    visible: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    updateCancel() {
      this.visible = false;
      this.$emit("updateCancel");
    },
    handleSubmit() {
      login({ username: this.username, password: this.password }).then(
        (res) => {
          console.log(res);
          if (res.data !== "") {
            this.$message.info("登陆成功");
            this.sendUsername();
            this.$emit("updateCancel");
            this.$emit("updateLogin");
          } else {
            this.$message.info("账号或密码有误");
          }
        }
      );
    },
    sendUsername: function () {
      bus.$emit("username", this.username);
      localStorage.setItem("username", this.username);
      this.$router.push("/about");
    },
  },
  // created() {
  //     this.visible=false
  // }
};
</script>

<style scoped>
</style>